source ("~/research/vbm-vulnerable-maine/R/construct_header.R")

## Vulnerable ballot arrival table
for (election2use in elections2use <- c("2012-GE", "2014-GE", "2016-GE", "2018-GE")) {
  
  start_date = election_dates[[election2use]] + as.difftime(-31, unit = "days")
  election_date_minus_one = election_dates[[election2use]] + as.difftime(-1, unit = "days")
  election_date = election_dates[[election2use]] + as.difftime(0, unit = "days")
  election_date_plus_one = election_dates[[election2use]] + as.difftime(1, unit = "days")
  election_date_plus_two = election_dates[[election2use]] + as.difftime(2, unit = "days")
  election_date_plus_three = election_dates[[election2use]] + as.difftime(3, unit = "days")
  
  end_date = election_date_plus_three

  seq2use <- data.frame(seq(from = start_date, to = end_date, by = 1))
  names(seq2use) <- "received_date"
  
  row2add <- abl_files[[election2use]] %>%
    filter(received_type == "MA",
           !is.na(received_date),
           received_date <= end_date,
           received_date >= start_date) %>%
    summarize(ballots = n(),
              election_date_minus_one = sum(received_date == election_date_minus_one),
              election_date = sum(received_date == election_date),
              election_date_plus_one = sum(received_date == election_date_plus_one),
              election_date_plus_two = sum(received_date == election_date_plus_two),
              election_date_plus_three = sum(received_date == election_date_plus_three)) %>%
    mutate(election_date_minus_one_percent = 100 * election_date_minus_one / ballots,
           election_date_percent = 100 * election_date / ballots,
           election_date_plus_one_percent = 100 * election_date_plus_one / ballots,
           election_date_plus_two_percent = 100 * election_date_plus_two / ballots,
           election_date_plus_three_percent = 100 * election_date_plus_three / ballots) 
  
  row2add_vulnerable <- row2add %>%
    select(ballots, election_date, election_date_minus_one, election_date_percent, election_date_minus_one_percent)
  
  row2add_late <- row2add %>%
    mutate(late_percent = 100 * (election_date_plus_one + election_date_plus_two + election_date_plus_three) / ballots) %>%
    select(ballots, election_date_plus_one, election_date_plus_two, election_date_plus_three,
           late_percent)
  
  if (match(election2use, elections2use) == 1) {
    fortable_vulnerable <- row2add_vulnerable
    fortable_late <- row2add_late
  }
  else {
    fortable_vulnerable <- rbind(fortable_vulnerable, row2add_vulnerable)
    fortable_late <- rbind(fortable_late, row2add_late)
  }
  
  matrix2add <- abl_files[[election2use]] %>%
    filter(received_type == "MA",
           enrollment %in% c("D", "R", "U"),
           !is.na(received_date),
           received_date <= end_date,
           received_date >= start_date) %>%
    mutate(vulnerable = 
             (received_date >= election_date_minus_one & received_date <= election_date)) %>%
    group_by(enrollment, vulnerable) %>%
    summarize(ballots = n()) %>%
    spread(enrollment, ballots)
  
  matrix2add_late <- abl_files[[election2use]] %>%
    filter(received_type == "MA",
           enrollment %in% c("D", "R", "U"),
           !is.na(received_date),
           received_date <= election_date_plus_three,
           received_date >= start_date) %>%
    mutate(late = 
             (received_date >= election_date_plus_one & received_date <= election_date_plus_three)) %>%
    group_by(enrollment, late) %>%
    summarize(ballots = n()) %>%
    spread(enrollment, ballots)
  
  matrix2add <- data.frame(t(matrix2add))
  matrix2add <- matrix2add[-1,]
  names(matrix2add) <- c("Not vulnerable", "Vulnerable")
  matrix2add$percent_vulnerable <-
    100 * matrix2add$Vulnerable / (matrix2add$`Not vulnerable` + matrix2add$`Vulnerable`)
  
  matrix2add_late <- data.frame(t(matrix2add_late))
  matrix2add_late <- matrix2add_late[-1,]
  names(matrix2add_late) <- c("Not barely late", "Barely late")
  matrix2add_late$percent_late <-
    100 * matrix2add_late$`Barely late` / (matrix2add_late$`Not barely late` + matrix2add_late$`Barely late`)
  
  if (match(election2use, elections2use) == 1) {
    fortable.party <- matrix2add
    fortable.party_late <- matrix2add_late
  }
  else {
    fortable.party <- rbind(fortable.party, matrix2add)
    fortable.party_late <- rbind(fortable.party_late, matrix2add_late)
  }
  
  matrix2add_large <- abl_files[[election2use]] %>%
    filter(municipality %in% c("FALMOUTH", "YORK", "BRUNSWICK", "SOUTH PORTLAND", "LEWISTON", "SCARBOROUGH", "BANGOR", "PORTLAND"),
           received_type == "MA",
           enrollment %in% c("D", "R", "U"),
           !is.na(received_date),
           received_date <= end_date,
           received_date >= start_date) %>%
    mutate(vulnerable = 
             (received_date >= election_date_minus_one & received_date <= election_date)) %>%
    group_by(enrollment, vulnerable) %>%
    summarize(ballots = n()) %>%
    spread(enrollment, ballots)
  
  matrix2add_large <- data.frame(t(matrix2add_large))
  matrix2add_large <- matrix2add_large[-1,]
  names(matrix2add_large) <- c("Not vulnerable", "Vulnerable")
  matrix2add_large$percent_vulnerable <- 100 * matrix2add_large$Vulnerable / (matrix2add_large$Vulnerable + matrix2add_large$`Not vulnerable`)
  
  if (match(election2use, elections2use) == 1) {
    fortable.party.large <- matrix2add_large
  }
  else {
    fortable.party.large <- rbind(fortable.party.large, matrix2add_large)
  }
}

## Print vulnerable ballots table
fortable_vulnerable <- cbind(elections2use, fortable_vulnerable)
names(fortable_vulnerable) <-
  c("Election", "Total", "Election Day", "Day before", "Election Day", "Day before")
fortable_vulnerable$Election <- gsub("-", " ", fortable_vulnerable$Election)

header2use <- construct_header(
  # the data.frame or matrix that should be plotted  
  fortable_vulnerable,
  # the labels of the groups that we want to insert
  grp_names = c("", "VBM ballots", "Percent"),
  # the number of columns each group spans
  span = c(1, 3, 2),
  # the alignment of each group, can be a single character (lcr) or a vector
  align = "c"
)

table2use <- xtable(fortable_vulnerable,
                    caption = "Vulnerable VBM ballots",
                    align = "llrrrrr",
                    digits = c(0,0,0,0,0,2,2),
                    label =  "tab:almost_late_vbm_ballots")

print.xtable(x = table2use, hline.after = FALSE, add.to.row = header2use, file = "/Users/herron/research/vbm-vulnerable-maine/tables/almost-late-vbm-ballots.tex", type = "latex", table.placement = "!ht", caption.placement = "top", include.colnames = TRUE, include.rownames = FALSE, format.args = list(big.mark = ","))

## Print late ballots table
fortable_late <- cbind(elections2use, fortable_late)
names(fortable_late) <-
  c("Election", "Total", "One", "Two", "Three", "Percent")
fortable_late$Election <- gsub("-", " ", fortable_late$Election)

header2use <- construct_header(
  # the data.frame or matrix that should be plotted  
  fortable_late,
  # the labels of the groups that we want to insert
  grp_names = c("", "Days late", "Percent"),
  # the number of columns each group spans
  span = c(2, 3, 1),
  # the alignment of each group, can be a single character (lcr) or a vector
  align = "c"
)

table2use <- xtable(fortable_late,
                    caption = "Late VBM ballots",
                    align = "llrrrrr",
                    digits = c(0,0,0,0,0,0,2),
                    label =  "tab:late_vbm_ballots")

print.xtable(x = table2use, hline.after = FALSE, add.to.row = header2use, file = "/Users/herron/research/vbm-vulnerable-maine/tables/late-vbm-ballots.tex", type = "latex", table.placement = "!ht", caption.placement = "top", include.colnames = TRUE, include.rownames = FALSE, format.args = list(big.mark = ","))

addtorow <- list(pos = list(0, 3, 6, 9),
                 command = c(paste0("\\hline\n\\multicolumn{4}{c}{\\textbf{2012 GE}}\\\\\n\\hline\n"),
                             paste0("\\hline\n\\multicolumn{4}{c}{\\textbf{2014 GE}}\\\\\n\\hline\n"),
                             paste0("\\hline\n\\multicolumn{4}{c}{\\textbf{2016 GE}}\\\\\n\\hline\n"),
                             paste0("\\hline\n\\multicolumn{4}{c}{\\textbf{2018 GE}}\\\\\n\\hline\n")))

# Print party table
fortable.party <- cbind(rownames(fortable.party), fortable.party)
names(fortable.party)[1] <- "Party"
names(fortable.party)[4] <- "Percent vulnerable"
fortable.party$Party <- rep(c("Democratic", "Republican", "Unaffiliated"), times = 4)
table2use <- xtable(fortable.party,
                    caption = "Partisanship and VBM ballot vulnerability",
                    align = "llrrr",
                    digits = c(0,0,0,0,2),
                    label =  "tab:partisanship_and_vulnerability")

print.xtable(x = table2use, hline.after = c(-1, nrow(fortable.party)), add.to.row = addtorow, file = "/Users/herron/research/vbm-vulnerable-maine/tables/partisanship_and_vulnerability.tex", type = "latex", table.placement = "!ht", caption.placement = "top", include.colnames = TRUE, include.rownames = FALSE, format.args = list(big.mark = ","))

# Print late party table
fortable.party_late <- cbind(rownames(fortable.party_late), fortable.party_late)
names(fortable.party_late)[1] <- "Party"
names(fortable.party_late)[4] <- "Percent late"
fortable.party_late$Party <- rep(c("Democratic", "Republican", "Unaffiliated"), times = 4)
table2use <- xtable(fortable.party_late,
                    caption = "Partisanship and late VBM ballots",
                    align = "llrrr",
                    digits = c(0,0,0,0,2),
                    label =  "tab:partisanship_and_lateness")

print.xtable(x = table2use, hline.after = c(-1, nrow(fortable.party)), add.to.row = addtorow, file = "/Users/herron/research/vbm-vulnerable-maine/tables/partisanship_and_lateness.tex", type = "latex", table.placement = "!ht", caption.placement = "top", include.colnames = TRUE, include.rownames = FALSE, format.args = list(big.mark = ","))

# Print party in large places table
fortable.party.large <- cbind(rownames(fortable.party.large), fortable.party.large)
names(fortable.party.large)[1] <- "Party"
names(fortable.party.large)[4] <- "Percent vulnerable"
fortable.party.large$Party <- rep(c("Democratic", "Republican", "Unaffiliated"), times = 4)
table2use <- xtable(fortable.party.large,
                    caption = "Partisanship and VBM ballot vulnerability, populous areas only",
                    align = "llrrr",
                    digits = c(0,0,0,0,2),
                    label =  "tab:partisanship_and_vulnerability_large_areas_only")

print.xtable(x = table2use, hline.after = c(-1, nrow(fortable.party)), add.to.row = addtorow, file = "/Users/herron/research/vbm-vulnerable-maine/tables/partisanship_and_vulnerability_large_areas_only.tex", type = "latex", table.placement = "!ht", caption.placement = "top", include.colnames = TRUE, include.rownames = FALSE, format.args = list(big.mark = ","))

## Party table
for (election2use in elections2use) {
  
  start_date = election_dates[[election2use]] + as.difftime(-31, unit = "days")
  end_date = election_dates[[election2use]] + as.difftime(0, unit = "days")
  
  seq2use <- data.frame(seq(from = start_date, to = end_date, by = 1))
  names(seq2use) <- "received_date"
  
  row2add <- abl_files[[election2use]] %>%
    filter(received_type == "MA",
           !is.na(received_date),
           received_date <= end_date,
           received_date >= start_date) %>%
    summarize(`VBM ballots` = n(),
              Democratic = sum(enrollment == "D"),
              Republican = sum(enrollment == "R"),
              Unaffiliated = sum(enrollment == "U"),
              Other = sum(enrollment %ni% c("D", "R", "U")))
              
  if (match(election2use, elections2use) == 1) {
    fortable <- row2add
  }
  else {
    fortable <- rbind(fortable, row2add)
  }
}

fortable <- cbind(elections2use, fortable)
names(fortable)[1] <- c("Election")
fortable$Election <- gsub("-", " ", fortable$Election)

table2use <- xtable(fortable,
                    caption = "VBM ballots and partisanship",
                    align = "llrrrrr",
                    digits = c(0,0,0,0,0,0,0),
                    label =  "tab:vbm_partisanship")

print.xtable(x = table2use, file = "/Users/herron/research/vbm-vulnerable-maine/tables/vbm-partisanship.tex", type = "latex", table.placement = "!ht", caption.placement = "top", include.colnames = TRUE, include.rownames = FALSE, format.args = list(big.mark = ","))


